package com.ny.controller;
import com.ny.entity.Classrooms;
import com.ny.entity.Users;

import com.ny.service.ClassroomService;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassroomService classroomService;
    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password,Model model,HttpSession session) {
        Users user = userService.login(username, password);
//        存储用户信息到session中
        session.setAttribute("user", user);

        if (user!= null) {
            List<Classrooms> classroomsList = classroomService.findAllClassrooms();
//            classroomsList:代表查询出来的数据。
//            "classroomsList"  model的名字。jsp 就是从中拿取数据。
//            查询出来的数据存放到model中，然后jsp 从model中取出来。jsp就可以获取到数据库查询出来的信息了
            model.addAttribute("classroomsList", classroomsList);
//            model.addAttribute("user", user);
            return "classroom";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "login";
        }
    }
// http:localhost:8082/
// 多条件筛选:  提交的路径:/user/searchClassrooms   2.提交的方法 get  3.请求数据：
    @RequestMapping("/searchClassrooms")
    public String  searchClassrooms(@RequestParam String classroomName,
                                    @RequestParam String capacity,
                                    @RequestParam String status,
                                    @RequestParam String building,
                                    @RequestParam String description,
                                    Model model){

        List<Classrooms>  classroomsList =  classroomService.searchClassrooms(classroomName,capacity,status,building,description);
        model.addAttribute("classroomsList",classroomsList);
        return "classroom";
    }
    @GetMapping("/person")
    public String personalCenter(HttpSession session, Model model) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId!= null) {
            Users user = userService.getUserById(userId);
            model.addAttribute("user", user);

        }
        return "personalCenter";
    }

    // 处理修改密码页面请求
    @GetMapping("/changePassword")
    public String changePasswordView() {
        return "changePassword";
    }


    @RequestMapping("/changePhoneNumber")
    public String changePhoneNumber(){
        return "changePhoneNumber";
    }
    @PostMapping("/changePassword1")
    public String changePassword(@RequestParam int userId, @RequestParam String password,
                                 Model model) {
        int result = userService.updateUserPassword(userId, password);
        if (result > 0) {
            model.addAttribute("message", "密码修改成功");
        } else {
            model.addAttribute("message", "密码修改失败");
        }
        return "changePassword";
    }
    @PostMapping("/changePhoneNumber1")
    public String changePhoneNumber(@RequestParam int userId, @RequestParam String phoneNumber,
                                    Model model) {
        boolean result = userService.updatePhoneNumber(userId, phoneNumber);
        if (result) {
            model.addAttribute("message", "手机号修改成功");
        } else {
            model.addAttribute("message", "手机号修改失败");
        }
        return "changePhoneNumber";
    }
}
